<?php

/**
 * 订单操作记录
 * File Name：MembersMoneyActionModel.php
 * File Encoding：UTF-8
 * File New Time：2014-6-4 14:01:56
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class MembersMoneyActionModel extends BaseModel {

    public function tableName() {
        return '{{members_money_action}}';
    }

    public function rules() {
        return array(
            array('uid', 'required', 'message' => '会员UID不能为空！'),
            array('money', 'required', 'message' => '支付金额不能为空！'),
            array('order_sn', 'required', 'message' => '订单编号不能为空！'),
            //无需验证
            array('type,action_time,note', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 添加订单操作记录
     * @param type $moneyPositionOrder 订单记录对象或者订单记录数组
     * @param type $type 类型
     * @param type $note 
     * @return boolean
     */
    public function addActionLog($order, $type, $note = '') {
        if (is_object($order)) {
            $order = $order->attributes;
        }
        if (!is_array($order) || empty($order)) {
            return false;
        }
        $data = array(
            'uid' => $order['uid'],
            'type' => $type,
            'note' => $note ? $note : $order['order_title'],
            'money' => $order['money'],
            'order_sn' => $order['order_sn'],
        );
        $this->attributes = $data;
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->save()) {
            return true;
        } else {
            return false;
        }
    }

    //数据保存前操作
    protected function beforeSave() {
        //是否新增
        if ($this->isNewRecord) {
            $this->action_time = time();
        } else {
            
        }
        return true;
    }

}
